package com.chenliang.sc.filter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @program: springcase
 * @description: 过滤器实现2,自定义过滤器,实现Filter接口,添加@WebFilter注解,配置拦截路径,启动类添加@ServletComponentScan
 * 缺点: 无法配置顺序
 * @author: 陈亮
 * @create: 2022-06-09 17:52
 */
@WebFilter(urlPatterns = {"/user/*"})
@ConditionalOnProperty(
        prefix = "config",
        name = {"filter.open"},
        havingValue = "true",
        matchIfMissing = false
)
public class MyFilter02 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("容器启动:初始化MyFilter02");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("MyFilter02.doFilter()执行");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("容器销毁:MyFilter02.destroy()执行");
    }
}
